<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-row>
        <el-col :span="4.5">
          <el-form-item label="所属板块" prop="areaId">
            <el-select v-model="queryParams.areaId" placeholder="请选择所属板块" clearable style="width: 180px;">
              <el-option
                  v-for="dict in area_id"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="4.5">
          <el-form-item label="合同项目名称" prop="projectName" label-width="100px">
            <el-input
                v-model="queryParams.projectName"
                placeholder="请输入合同项目名称"
                clearable
                @keyup.enter="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="4.5">
          <el-form-item label="合同编号" prop="projectCode" label-width="74px">
            <el-input
                v-model="queryParams.projectCode"
                placeholder="请输入合同编号"
                clearable
                @keyup.enter="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="4.5">
          <el-form-item label="签约合同价" prop="contract" label-width="89px">
            <el-input
                v-model="queryParams.contract"
                placeholder="请输入签约合同价"
                clearable
                @keyup.enter="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="4.5">
          <el-form-item label="签约时间" style="width: 308px" label-width="89px">
            <el-date-picker
                v-model="daterangeContractTime"
                value-format="YYYY-MM-DD"
                type="daterange"
                range-separator="-"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
            ></el-date-picker>
          </el-form-item>
        </el-col>
        <!--<el-col :span="4.5">
          <el-form-item label="项目所在地" prop="contractIn" label-width="100px">
            <el-input
                v-model="queryParams.contractIn"
                placeholder="请输入项目所在地"
                clearable
                @keyup.enter="handleQuery"
            />
          </el-form-item>
        </el-col>-->

      </el-row>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="primary"
            plain
            icon="Plus"
            @click="handleAdd"
            v-hasPermi="['system:contract:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="success"
            plain
            icon="Edit"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['system:contract:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="danger"
            plain
            icon="Delete"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['system:contract:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="warning"
            plain
            icon="Download"
            @click="handleExport"
            v-hasPermi="['system:contract:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="contractList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="" align="center" prop="id" v-if="false"/>
      <el-table-column label="地区" align="center" prop="areaId" v-if="false" />
      <el-table-column label="所属板块" align="center" prop="areaId" sortable>
        <template #default="scope">
          <dict-tag :options="area_id" :value="scope.row.areaId"/>
        </template>
      </el-table-column>
      <el-table-column label="合同项目名称" align="center" prop="projectName" sortable
                       :sort-method="nameSort"/>
      <el-table-column label="合同编号" align="center" prop="projectCode" sortable/>
      <el-table-column label="建设单位" align="center" prop="constructName" sortable
                       :sort-method="constructSort"/>
      <el-table-column label="签约合同价" align="center" prop="contract" sortable/>
      <el-table-column label="签约时间" align="center" prop="contractTime" width="180" sortable>
        <template #default="scope">
          <span>{{ parseTime(scope.row.contractTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="项目所在地" align="center" prop="contractIn" sortable
                       :sort-method="contractInSort"/>
      <el-table-column label="备注" align="center" prop="remark" sortable/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['system:contract:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['system:contract:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />

    <!-- 添加或修改新签合同对话框 -->
    <el-dialog :title="title" v-model="open" width="600px" append-to-body>
      <el-form ref="contractRef" :model="form" :rules="rules" label-width="140px">
        <el-form-item label="所属板块" prop="areaId" label-width="140px">
          <el-select v-model="form.areaId" placeholder="请选择所属板块" style="width: 200px;">
            <el-option
                v-for="dict in area_id"
                :key="dict.value"
                :label="dict.label"
                :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="合同项目名称" prop="projectName">
          <el-input v-model="form.projectName" placeholder="请输入合同项目名称"  style="width: 340px;" />
        </el-form-item>
        <el-form-item label="合同编号" prop="projectCode">
          <el-input v-model="form.projectCode" placeholder="请输入合同编号"  style="width: 340px;" />
        </el-form-item>
        <el-form-item label="建设单位" prop="constructName">
          <el-input v-model="form.constructName" placeholder="请输入建设单位"  style="width: 340px;" />
        </el-form-item>
        <el-form-item label="签约合同价" prop="contract">
          <el-input v-model="form.contract" placeholder="请输入签约合同价"  style="width: 340px;" />
        </el-form-item>
        <el-form-item label="签约时间" prop="contractTime">
          <el-date-picker clearable
                          v-model="form.contractTime"
                          type="date"
                          value-format="YYYY-MM-DD"
                          placeholder="请选择签约时间" style="width: 170px;" >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="项目所在地" prop="contractIn">
          <el-input v-model="form.contractIn" placeholder="请输入项目所在地"  style="width: 340px;" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注"  style="width: 340px;" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Contract">
import { listContract, getContract, delContract, addContract, updateContract } from "@/api/system/contract";

const { proxy } = getCurrentInstance();
const { area_id } = proxy.useDict('area_id');

const contractList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const daterangeContractTime = ref([]);

const nameSort = (a, b) => {
  return a.projectName.localeCompare(b.projectName);
};
const constructSort = (a, b) => {
  return a.constructName.localeCompare(b.constructName);
};
const contractInSort = (a, b) => {
  return a.contractIn.localeCompare(b.contractIn);
};
const remarkSort = (a, b) => {
  return a.remark.localeCompare(b.remark,'zh');
};

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    areaId: null,
    projectName: null,
    projectCode: null,
    constructName: null,
    contract: null,
    contractTime: null,
    contractIn: null,
  },
  rules: {
    areaId: [
      { required: true, message: "地区不能为空", trigger: "blur" }
    ],
    projectName: [
      { required: true, message: "合同项目名称不能为空", trigger: "blur" }
    ],
    projectCode: [
      { required: true, message: "合同编号不能为空", trigger: "blur" }
    ],
    constructName: [
      { required: true, message: "建设单位不能为空", trigger: "blur" }
    ],
    contract: [
      { required: true, message: "签约合同价不能为空", trigger: "blur" }
    ],
    contractTime: [
      { required: true, message: "签约时间不能为空", trigger: "blur" }
    ],
    contractIn: [
      { required: true, message: "项目所在地不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询新签合同列表 */
function getList() {
  loading.value = true;
  queryParams.value.params = {};
  if (null != daterangeContractTime && '' !== daterangeContractTime) {
    queryParams.value.params["beginContractTime"] = daterangeContractTime.value[0];
    queryParams.value.params["endContractTime"] = daterangeContractTime.value[1];
  }
  listContract(queryParams.value).then(response => {
    contractList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    areaId: null,
    projectName: null,
    projectCode: null,
    constructName: null,
    contract: null,
    contractTime: null,
    contractIn: null,
    remark: null
  };
  proxy.resetForm("contractRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  daterangeContractTime.value = [];
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length !== 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加新签合同";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getContract(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改新签合同";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["contractRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateContract(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addContract(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除新签合同编号为"' + _ids + '"的数据项？').then(function() {
    return delContract(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('system/contract/export', {
    ...queryParams.value
  }, `contract_${new Date().getTime()}.xlsx`)
}

getList();
</script>

